package system

import (
	"gin-admin/common/dto/request"
	"gin-admin/common/dto/response"
	"gin-admin/core"
	"gin-admin/global"
	serviceRequest "gin-admin/service/dto/request"
	"gin-admin/service/v1/system"

	"github.com/gin-gonic/gin"
)

type UserHandler struct {
	UserService system.UserService
}

var UserHandlerApp = &UserHandler{
	UserService: *system.UserServiceApp,
}

// @Summary 创建用户信息
// @Description 创建用户信息接口
// @Tags /v1/用户管理
// @Accept json
// @Product json
// @Param data body request.CreateUserRequest true "创建用户信息请求参数"
// @Router /api/v1/user/create [post]
func (uc *UserHandler) CreateUserHandler(ctx *gin.Context) {
	var createUserRequest serviceRequest.CreateUserRequest
	if !core.ValidateParams(ctx, &createUserRequest) {
		return
	}
	if is_admin := ctx.GetBool("is_admin"); !is_admin && createUserRequest.Account != "admin" {
		response.FailedWithCode(ctx, response.SystemPermissionCode)
		return
	}
	_, err := CaptchaHandlerApp.CaptchaService.VerifyCaptcha(createUserRequest.CaptchaId, createUserRequest.Captcha)
	if err != nil {
		response.FailedWithCode(ctx, response.CaptchaInvalidCode)
		return
	}
	if err := uc.UserService.CreateUserService(&createUserRequest); err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.SuccessNoData(ctx)
}

// @Summary 删除用户信息
// @Description 删除用户信息接口
// @Tags /v1/用户管理
// @Accept json
// @Product json
// @Param data body request.IDRequest true "删除用户请求参数"
// @Router /api/v1/user/delete [delete]
func (uc *UserHandler) DeleteUserHandler(ctx *gin.Context) {
	var iDRequest request.IDRequest
	if !core.ValidateParams(ctx, &iDRequest) {
		return
	}
	if err := uc.UserService.DeleteUserService(&iDRequest); err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.SuccessNoData(ctx)
}

// @Summary 修改用户信息
// @Description 修改用户信息接口
// @Tags /v1/用户管理
// @Accept json
// @Product json
// @Param data body request.ModifyUserRequest true "修改用户信息请求参数"
// @Router /api/v1/user/modify [put]
func (uc *UserHandler) ModifyUserHandler(ctx *gin.Context) {
	var modifyUserRequest serviceRequest.ModifyUserRequest
	if !core.ValidateParams(ctx, &modifyUserRequest) {
		return
	}
	err := uc.UserService.ModifyUserService(&modifyUserRequest)
	if err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.SuccessNoData(ctx)
}

// @Summary 获取用户详情
// @Description 获取用户详情接口
// @Tags /v1/用户管理
// @Product json
// @Param id path string true "获取用户详情请求参数"
// @Router /api/v1/user/info/{id} [get]
func (uc *UserHandler) GetUserInfoHandler(ctx *gin.Context) {
	id := ctx.Param("id")
	if !core.ValidateParams(ctx, id) {
		return
	}
	result, err := uc.UserService.GetUserInfoService(id)
	if err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.Success(ctx, result)
}

// @Summary 获取用户列表
// @Description 获取用户列表接口
// @Tags /v1/用户管理
// @Accept json
// @Product json
// @Param data body request.UserListRequest true "用户列表请求参数"
// @Router /api/v1/user/list [get]
func (uc *UserHandler) GetUserListHandler(ctx *gin.Context) {
	var userListRequest serviceRequest.UserListRequest
	if err := ctx.ShouldBindJSON(&userListRequest); err != nil {
		global.GA_LOGGER.Sugar().Errorw("参数校验失败", "error", err)
		response.Failed(ctx)
		return
	}
	result, err := uc.UserService.GetUserListService(&userListRequest)
	if err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.SuccessWithList(ctx, result.Page, result.Size, result.Total, result.List)
}

// @Summary 用户注册
// @Description 用户注册接口
// @Tags /v1/用户管理
// @Accept json
// @Product json
// @Router /api/v1/user/register [post]
func (uc *UserHandler) RegisterHandler(ctx *gin.Context) {
	ctx.JSON(200, gin.H{
		"message": "注册成功",
	})
}

// @Summary 用户登录
// @Description 用户登录接口
// @Tags /v1/用户管理
// @Accept json
// @Product json
// @Param data body request.LoginRequest true "用户登录请求参数"
// @Router /api/v1/user/login [post]
func (uc *UserHandler) LoginHandler(ctx *gin.Context) {
	var loginRequest serviceRequest.LoginRequest
	if !core.ValidateParams(ctx, &loginRequest) {
		return
	}
	if verify, err := CaptchaHandlerApp.CaptchaService.VerifyCaptcha(loginRequest.CaptchaId, loginRequest.Captcha); err != nil && !verify {
		response.FailedWithCode(ctx, response.CaptchaInvalidCode)
		return
	}
	result, err := uc.UserService.LoginService(&loginRequest)
	if err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.Success(ctx, result)
}
